<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2021/6/11
 * Time: 17:04
 */

namespace app\api\controller;
use app\common\controller\Api;

use fast\Encrypt;

class Base extends Api
{
    public function _initialize() {
        parent::_initialize();
    }

    /**
     * @param $openid
     * @return string
     */
    protected function getUserId($openid){
        $userId = "";
        $openIdString = Encrypt::decrypt($openid);
        if(!empty($openid)){
            $openIdArr = explode('_',$openIdString);
            $userId = $openIdArr[count($openIdArr)-1];
        }
        return $userId;
    }

    protected function chckedIsLogin($param){
        $openid = $param['openid'];
        if(empty($openid)){
            $this->customError("请先登录！",9999);
        }
        $userId = $this->getUserId($openid);
        if(empty($userId)){
            $this->error("登录状态不正确！");
        }
        return $userId;
    }

    protected function customError($msg = '', $code=0,$url = null, $data = '', $wait = 3, array $header = []){
        $this->error($msg, $url, $data, $wait, $header,$code);
//        $this->result($msg, $data, $code, $type, $header);
    }

    protected function mb_str_split($str, $count){
        $leng = strlen($str)/3;     //中文长度
        $arr = array();
        for ($i=0; $i < $leng; $i+=$count) {
            $arr[] = mb_substr($str, $i, $count);
        }
        return $arr;
    }
}